/**
 * 添加医院站点数据到 MongoDB
 * 确保 h5 前端能够从真实数据库获取医院数据
 */

const mongoose = require('mongoose');
// 需要先编译 TypeScript 或使用 ts-node
const { HospitalSite } = require('./src/models/mongodb-schemas.ts');
require('dotenv').config();

// 医院站点数据
const hospitalSitesData = [
  {
    siteId: "h001",
    name: "河北省保定市莲池区保定市第一医院",
    address: "河北省保定市莲池区东风东路572号",
    coordinates: {
      longitude: 115.4882,
      latitude: 38.8664
    },
    zoom: 16,
    district: "莲池区",
    city: "保定市",
    province: "河北省",
    status: "active",
    description: "保定市重点三甲医院",
    contactInfo: {
      phone: "0312-5976120",
      website: "http://www.bdyy.cn"
    },
    facilities: ["急诊科", "住院部", "门诊部", "检验科", "影像科"],
    operatingHours: {
      weekdays: "8:00-17:30",
      weekends: "8:00-16:00",
      holidays: "急诊24小时"
    },
    capacity: {
      beds: 1200,
      departments: 25,
      doctors: 180
    }
  },
  {
    siteId: "h002",
    name: "河北省保定市竞秀区保定市人民医院",
    address: "保定市竞秀区长城北大街320号",
    coordinates: {
      longitude: 115.4544,
      latitude: 38.9069
    },
    zoom: 16,
    district: "竞秀区",
    city: "保定市",
    province: "河北省",
    status: "active",
    description: "保定市综合性三甲医院",
    contactInfo: {
      phone: "0312-5976666",
      website: "http://www.bdrmyy.com"
    },
    facilities: ["急诊科", "住院部", "门诊部", "手术室", "ICU"],
    operatingHours: {
      weekdays: "8:00-17:30",
      weekends: "8:00-16:00",
      holidays: "急诊24小时"
    },
    capacity: {
      beds: 1000,
      departments: 22,
      doctors: 150
    }
  },
  {
    siteId: "h003",
    name: "保定市中医院",
    address: "保定市莲池区裕华东路530号",
    coordinates: {
      longitude: 115.5123,
      latitude: 38.8756
    },
    zoom: 16,
    district: "莲池区",
    city: "保定市",
    province: "河北省",
    status: "active",
    description: "保定市中医特色三甲医院",
    contactInfo: {
      phone: "0312-5976888",
      website: "http://www.bdzyy.com"
    },
    facilities: ["中医内科", "针灸科", "推拿科", "中药房"],
    operatingHours: {
      weekdays: "8:00-17:30",
      weekends: "8:00-16:00",
      holidays: "急诊24小时"
    },
    capacity: {
      beds: 800,
      departments: 18,
      doctors: 120
    }
  }
];

async function seedHospitalSites() {
  try {
    // 连接到 MongoDB - 使用正确的线上数据库
    const mongoUri = process.env.MONGODB_URI || 'mongodb+srv://1191665779:20020219aa@zn.apmsejq.mongodb.net/medical_digital_twin';
    console.log('连接到 MongoDB:', mongoUri);
    
    await mongoose.connect(mongoUri, {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    });

    console.log('MongoDB 连接成功');

    // 清空现有数据（可选）
    console.log('清空现有医院站点数据...');
    await HospitalSite.deleteMany({});

    // 插入新数据
    console.log('插入新的医院站点数据...');
    const insertedSites = await HospitalSite.insertMany(hospitalSitesData);
    
    console.log(`成功插入 ${insertedSites.length} 个医院站点:`);
    insertedSites.forEach(site => {
      console.log(`- ${site.name} (${site.siteId})`);
    });

    // 验证数据
    const count = await HospitalSite.countDocuments();
    console.log(`\n数据库中现有医院站点数量: ${count}`);

    console.log('\n✅ 医院站点数据迁移完成！');
    
  } catch (error) {
    console.error('❌ 医院站点数据迁移失败:', error);
  } finally {
    // 关闭数据库连接
    await mongoose.disconnect();
    console.log('数据库连接已关闭');
  }
}

// 运行脚本
if (require.main === module) {
  seedHospitalSites();
}

module.exports = { seedHospitalSites };